import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import LabelEncoder

def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
    n_vars = 1 if type(data) is list else data.shape[1]
    df = pd.DataFrame(data)
    cols, names = list(), list()
    
    # 输入序列 (t-n, ... t-1)
    for i in range(n_in, 0, -1):
        cols.append(df.shift(i))
        names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
    
    # 预测序列 (t, t+1, ... t+n)
    for i in range(0, n_out):
        cols.append(df.shift(-i))
        if i == 0:
            names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
        else:
            names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
    
    # 合并到一起
    agg = pd.concat(cols, axis=1)
    agg.columns = names
    
    # 删除NaN行
    if dropnan:
        agg.dropna(inplace=True)
    return agg

def species_data_process(values):
    #标签编码 integer encode direction
    encoder = LabelEncoder()
    values= encoder.fit_transform(values)
    print('标签编码') 
    print(values)
    #使所有物种数量数据是float类型
    values = values.astype('float32')

    # 归一化 normalize features
    scaler = MinMaxScaler(feature_range=(0, 1))
    values = values.reshape(-1, 1)  # 将一维数组转换为二维数组
    scaled = scaler.fit_transform(values)
    print('缩放') 
    print(scaled) 

    # 转化为有监督数据
    reframed = series_to_supervised(scaled, 1, 1)
    return reframed, scaler